﻿function EditRecordIdentyficator(jqLabel, jqInput) {
    this.UI = new Object();
    this.UI.Label = $(jqLabel);
    this.UI.Input = $(jqInput);
};

EditRecordIdentyficator.prototype.ShowPopup = function () {
    var controller = this;
    var $dialogCont = $('<div id="edit-identyficator" title="Edytuj identyfikator"><p>Wprowadź nowy identyfikator</p><textarea></textarea></div>');
    $dialogCont.find('textarea').val(this.UI.Label.text());
    $dialogCont.dialog({
        resizable: false,
        height: 230,
        modal: true,
        buttons: {
            "Anuluj": function () {
                $(this).dialog("close");
            },
            "Zapisz": function () {
                var txt = $dialogCont.find('textarea').val();
                controller.UI.Label.text(txt);
                controller.UI.Label.attr('title', txt);
                controller.UI.Input.val(txt);
                $(this).dialog("close");
            }
        }
    });
};
function ReferencesController(jqElement) {
    this.Handler = $(jqElement);
    this.Items = new Array();
    this.ReferenceType = 'one';
    if (this.Handler.hasClass('references-manyreference'))
        this.ReferenceType = 'many';

    this.Handler.data('controller', this);

    this.Handler.parent().find('a[data-type=open-popup]').data('controller', this).click(function () {
        window.CurrentReferencesController = $(this).data('controller');
    });
};
ReferencesController.prototype.SetItems = function (preSelecteds) {
    var controller = this;
    this.Handler.find('.row').each(function () {
        var item = new ReferenceItem(controller, this);
        if (preSelecteds && preSelecteds.indexOf(item.RecordId) >= 0)
            item.SetState(true);
        controller.Items.push(item);
    });
};
ReferencesController.prototype.Refresh = function () {
    var selecteds = new Array();
    for (var xs = 0; xs < this.Items.length; xs++) {
        if (this.Items[xs].Selected)
            selecteds.push(this.Items[xs].RecordId);
    }
    var parent = this.Handler.parent();
    parent.html('<div class="references-reload">Odświeżam listę ...</div>');
    var url = window.location.href + ' #' + parent.attr('id');
    parent.load(url, function () {
        var rc = new ReferencesController($(this).find('.edit-references'));
        rc.SetItems(selecteds);
        window.Nemo.NemoPopup.Reload(parent);
    });
};
ReferencesController.prototype.ChangeState = function (item) {
    if (this.ReferenceType == 'one')
        this.__ChangeStateOneRef(item);
    else
        this.__ChangeStateManyRef(item);
};
ReferencesController.prototype.__ChangeStateOneRef = function (item) {
    if (item.Selected) return;
    for (var xi = 0; xi < this.Items.length; xi++) {
        this.Items[xi].SetState(this.Items[xi].RecordId == item.RecordId);
    }
};
ReferencesController.prototype.__ChangeStateManyRef = function (item) {
    item.SetState(!item.Selected);
};
function ReferenceItem(controller, jqRow) {
    this.Controller = controller;
    this.UI = new Object();
    this.UI.Row = $(jqRow);
    this.UI.Input = this.UI.Row.find('.check input');
    this.Selected = false;
    this.RecordId = parseInt(this.UI.Row.attr('data-recordid'));
    this.Init();
};
ReferenceItem.prototype.Init = function () {
    if (this.UI.Input.is(':checked')) {
        this.Selected = true;
        this.UI.Row.addClass('row-checked');
    }

    this.UI.Row.data('item', this).css('cursor', 'pointer').click(function () {
        var item = $(this).data('item');
        item.Controller.ChangeState(item);
    });
    this.UI.Row.find('.actions a').click(function (e) {
        e.stopPropagation();
    });
};

ReferenceItem.prototype.SetState = function (state) {
    if (this.Selected == state) return;
    this.Selected = state;
    if (this.Selected) {
        this.UI.Row.addClass('row-checked');
        this.UI.Input.prop('checked', true);
    }
    else {
        this.UI.Row.removeClass('row-checked');
        this.UI.Input.prop('checked', false);
    }
};

window.AfterCloseEditRecord = function () {
    if (window.CurrentReferencesController)
        window.CurrentReferencesController.Refresh();
};

$(function () {
    $('#recordEditIdent a.edit').click(function (e) {
        e.preventDefault();
        var popup = new EditRecordIdentyficator($('#recordEditIdent a.text'), $('#recordEditIdent input'));
        popup.ShowPopup();
    });

    $('.edit-references').each(function () {
        var controller = new ReferencesController(this);
        controller.SetItems();
    });


    // SUPPORT FOR YES-NO SELECTOR
    $('table.record-yesno td').each(function () {
        var $td = $(this);
        var $input = $td.find('input');
        if ($input.is(':checked'))
            $td.addClass('selected');

        $input.change(function () {
            $(this).parent().parent().find('td').removeClass('selected');
            $(this).parent().addClass('selected');
        });
    });


    // SUPPORT FOR FILE
    $('input[type=file]').prettyFileInput({
        'button-class': 'button'
    });

    // SUPPORT DATE
    $('.inputdate,.inputtime').focus(function () {
        if ($(this).val() == $(this).prop('defaultValue'))
            $(this).val('');
    }).blur(function () {
        if ($(this).val() == '')
            $(this).val($(this).prop('defaultValue'));
    });

    $('.inputdate').datepicker({ dateFormat: 'd MM, yy' });
    $('.inputdate[orgdate]').each(function () {
        $(this).datepicker('setDate', $.datepicker.parseDate('dd-mm-yy', $(this).attr('orgdate')));
    });

    $('.inputtime').keyfilter(function (c) {
        c = $(this).val() + c;
        if (c.length >= 1) {
            if (c[0] != '0' && c[0] != '1' && c[0] != '2')
                return false;
        }
        if (c.length >= 2) {
            var ci = parseInt(c[1]);
            if (ci + '' != c[1]) return false;
            if (ci < 0 || ci > 9)
                return false;
        }
        if (c.length >= 3) {
            if (c[2] != ':')
                return false;
        }
        if (c.length >= 4) {
            var ci = parseInt(c[3]);
            if (ci + '' != c[3]) return false;
            if (ci < 0 || ci > 5)
                return false;
        }
        if (c.length >= 5) {
            var ci = parseInt(c[4]);
            if (ci + '' != c[4]) return false;
            if (ci < 0 || ci > 9)
                return false;
        }
        if (c.length > 5)
            return false;
        return true;
    });

});
